package com.superm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.superm.entity.*;
import com.superm.mappers.*;
import com.superm.service.GoodsService;
import com.superm.service.ShelfApplicationService;
import com.superm.utils.Tool;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 上架申请单业务实现类
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class ShelfApplicationServiceImpl implements ShelfApplicationService {

    @Autowired
    private ShelfApplicationMapper shelfApplicationMp;

    @Autowired
    private ShelfDetailsMapper shelfDetailsMp;


    @Autowired
    private RepostoryMapper repostoryMapper;


    @Autowired
    private ShelfComeoutMapper shelfComeoutMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private StorgageRackMapper storgageRackMapper;

    @Autowired
    private GoodsMapper goodsMapper;




    /**
     * 实现添加上架申请单
     * @param shelfApplicationPoJo
     * @return
     */
    @Override
    public Map<String, Object> addShelfApplication(ShelfApplicationPoJo shelfApplicationPoJo, ShelfDetailsPoJo shelfDetailsPoJo,HttpSession httpSession){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取认证主体
        Subject subjet = SecurityUtils.getSubject();
        //从认证主体取出认证信息
        Emp emp = (Emp)subjet.getPrincipal();
        map.put("code",0);
        //替换值
        ShelfApplication shelfApplication = new ShelfApplication();
        BeanUtils.copyProperties(shelfApplicationPoJo,shelfApplication);
        //仓库替换
        shelfApplication.setRepositoryId(new RepostoryType(shelfApplicationPoJo.getRepositoryId()));
        //经办人替换
        shelfApplication.setEmpId(new Emp(emp.geteId()));
        //编号随机数生成
        shelfApplication.setSapplicationCode(Tool.dateRandom("SJ"));
        shelfApplication.setShelfappType("上架");
        shelfApplication.setSapplicationGoodsCount(shelfDetailsPoJo.getShelfCount());
        if (shelfApplicationMp.addRequistion(shelfApplication)>0){
            //添加详情
            ShelfDetails shelfDetails  = new ShelfDetails();
            BeanUtils.copyProperties(shelfDetailsPoJo,shelfDetails);
            //商品库存
            shelfDetails.setStock(new Stock(shelfDetailsPoJo.getStock()));
            //货架
            shelfDetails.setStorgageRack(new StorgageRack(shelfDetailsPoJo.getStorgageRack()));
            //申请单编号
            shelfDetails.setShelfApplication(shelfApplication);
            //执行添加详情
            if(shelfDetailsMp.addRequistionDetails(shelfDetails)>0){
                map.put("msg","申请成功");
                map.put("success",true);
            }
        }
        return map;
    }

    /**
     * 实现多上架申请单添加
     * @param shelfApplicationPoJo
     * @param shelfApplicationPoJoList
     * @return
     */
    @Override
    public Map<String, Object> addShelfapplications(ShelfApplicationPoJo shelfApplicationPoJo,List<String> shelfApplicationPoJoList) {

        Map<String,Object> map = new HashMap<String,Object>();
        //获取认证主体
        Subject subjet = SecurityUtils.getSubject();
        //从认证主体取出认证信息
        Emp emp = (Emp)subjet.getPrincipal();
        //将详情信息转换为要存储的集合信息
        List<ShelfDetailsPoJo> shelfDetailsPoJos = JSONArray.parseArray(shelfApplicationPoJoList.toString(),ShelfDetailsPoJo.class);
        //替换值
        ShelfApplication shelfApplication = new ShelfApplication();
        BeanUtils.copyProperties(shelfApplicationPoJo,shelfApplication);
        //仓库替换
        shelfApplication.setRepositoryId(new RepostoryType(shelfApplicationPoJo.getRepositoryId()));
        //经办人替换
        shelfApplication.setEmpId(new Emp(emp.geteId()));
        //编号随机数生成
        shelfApplication.setSapplicationCode(Tool.dateRandom("SJ"));
        //总数
        shelfApplication.setSapplicationGoodsCount(shelfApplicationPoJo.getSapplicationGoodsCount());
        //类型
        shelfApplication.setShelfappType(shelfApplicationPoJo.getShelfappType());
        System.out.println(   shelfApplication.getShelfappType());
        if (shelfApplicationMp.addRequistion(shelfApplication)>0){
            //添加成功后循环添加详情信息
            shelfDetailsPoJos.forEach(shelfDetail->{
                //添加详情
                ShelfDetails shelfDetails  = new ShelfDetails();
                BeanUtils.copyProperties(shelfDetail,shelfDetails);
                //商品库存
                shelfDetails.setStock(new Stock(shelfDetail.getStock()));
                //货架
                shelfDetails.setStorgageRack(new StorgageRack(shelfDetail.getStorgageRack()));
                //申请单编号
                shelfDetails.setShelfApplication(shelfApplication);
                shelfDetailsMp.addRequistionDetails(shelfDetails);
            });
            //添加详情信息成功,返回提示信息
            map.put("msg","申请成功");
            map.put("success",true);
        }

        return map;
    }

    /**
     * 实现模糊查询上架申请单
     * @param shelfApplicationPoJo
     * @return
     */
    @Override
    public Map<String, Object> likeShelfApplication(ShelfApplicationPoJo shelfApplicationPoJo,Integer pageNo,Integer pageSize) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        //替换值
        ShelfApplication shelfApplication = new ShelfApplication();
        BeanUtils.copyProperties(shelfApplicationPoJo,shelfApplication);
        //经办人替换
        shelfApplication.setEmpId(new Emp(shelfApplicationPoJo.getEName()));
        List<ShelfApplication> shelfApplications = shelfApplicationMp.selectMationInfo(shelfApplication,(pageNo-1)*pageSize,pageSize);
        //查条数
        int count  =  shelfApplicationMp.selectCount(shelfApplication);
        map.put("count",count);
        map.put("data",shelfApplications);
        return map;
    }

    /**
     * 实现查询总条数
     * @param shelfApplicationPoJo
     * @return
     */
    @Override
    public int selectShelfApplicationCount(ShelfApplicationPoJo shelfApplicationPoJo) {
        //替换值
        ShelfApplication shelfApplication = new ShelfApplication();
        BeanUtils.copyProperties(shelfApplicationPoJo,shelfApplication);
        //经办人替换
        shelfApplication.setEmpId(new Emp(shelfApplicationPoJo.getEName()));
        return shelfApplicationMp.selectCount(shelfApplication);
    }

    /**
     * 实现审批通过
     * @param sapplicationCode
     * @return
     */
    @Override
    public  Map<String,Object>  approvalBy(String sapplicationCode){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (shelfApplicationMp.updateState1(sapplicationCode)>0){
            map.put("success",true);
            map.put("msg","审批成功!");
        }
        return map;
    }

    /**
     * 实现审批驳回
     * @param shelfApplication
     * @return
     */
    @Override
    public Map<String, Object> rejectApplyFor(ShelfApplication shelfApplication){
        shelfApplication.setRemarks("经理驳回:"+shelfApplication.getRemarks());
         Map<String,Object> map = new HashMap<String,Object>();
            map.put("code",0);
            if (shelfApplicationMp.updateState4(shelfApplication)>0){
                //驳回成功
                map.put("success",true);
                map.put("msg","已驳回");
            }
        return map;
    }

    /**
     * 实现上架出库改变状态生成出库单
     * @param sapplicationCode
     * @return
     */
    @Override
    public Map<String, Object> putInStorage(String sapplicationCode,String remarks) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        //根据单号查询到详情求出来共需容量,对仓库所占容量增加,生成出库单,改变状态
         double  totalCapacity =0.0;
         Integer stockCount = 0;
       //根据订单号查询指定订单信息
       ShelfApplication shelfApplication = shelfApplicationMp.selectCodeMationInfo(sapplicationCode);
        //查询所有详情信息
       List<ShelfDetails>  detailsList  = shelfDetailsMp.selectCodeDetails(sapplicationCode,null,null);
       //循环所有详情得出共需容量
        for (ShelfDetails de:detailsList){
          totalCapacity+=de.getCapacity();
          stockCount+=de.getShelfCount();
          //把每个详情的信息对库存减少数量
            stockMapper.decreaseStock(de.getStock().getStockId(),de.getShelfCount());

        }
        //对仓库已经容量增加
        if (repostoryMapper.ZRepcapCityxhao(totalCapacity,shelfApplication.getRepositoryId().getReponseTypePid())>0){
            //获取认证主体
            Subject subjet = SecurityUtils.getSubject();
            //从认证主体取出认证信息
            Emp emp = (Emp)subjet.getPrincipal();
            ShelfComeout shelfComeout = new ShelfComeout();
            shelfComeout.setShelfApplication(new ShelfApplication(sapplicationCode));
            shelfComeout.setShelfComeoutCount(stockCount);
            shelfComeout.setEmpId(new Emp(emp.geteId()));
            shelfComeout.setRemarks(remarks);

           //生成出库单
            if (shelfComeoutMapper.addShelfComeout(shelfComeout)>0){
                //修改状态已出库
                if ( shelfApplicationMp.updateState2(sapplicationCode)>0){
                    map.put("success",true);
                    map.put("msg","出库成功!");
                }
            }
        }
        return map;
    }


    /**
     * 实现商品上架
     * @param sapplicationCode
     * @return
     */
    @Override
    public Map<String,Object> goodsShelves(String sapplicationCode){
        //根据订单号查到所有详情,先对货架容量减少,然后对库存的商品改变状态增加数量
        Map<String,Object> map = new HashMap<String,Object>();
        //根据订单号查询指定订单信息
        ShelfApplication shelfApplication = shelfApplicationMp.selectCodeMationInfo(sapplicationCode);
        //查询所有详情信息
        List<ShelfDetails>  detailsList  = shelfDetailsMp.selectCodeDetails(sapplicationCode,null,null);

        //循环详情信息对货架进行容量减少
        for (ShelfDetails de:detailsList){
            System.out.println(de);
            //增加货架已用容量  id 数量
            storgageRackMapper.minusCaoacity(de.getStorgageRack().getStorgageRackId(),de.getCapacity());
            //对商品上架并且加数量和所属货架
            goodsMapper.goodsShelves(new Goods(de.getStock().getGoodsid(),de.getShelfCount(),new StorgageRack(de.getStorgageRack().getStorgageRackId())));
        }
            //把申请单的状态进行改变
            shelfApplicationMp.updateState3(sapplicationCode);
            map.put("success",true);
            map.put("msg",shelfApplication.getShelfappType()+"成功");
        return map;
    }


}
